package com.cisystem.common.utils.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ExcelDataListener implements ReadListener<ExcelFileData> {
    private static final Logger log = LoggerFactory.getLogger(ExcelDataListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;

    private List<ExcelFileData> cachedDataList ;

    public ExcelDataListener(List<ExcelFileData> cachedDataList) {
        if (cachedDataList == null){
            cachedDataList = ListUtils.newArrayList();
        }
        this.cachedDataList = cachedDataList;
    }
    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(ExcelFileData data, AnalysisContext context) {
        cachedDataList.add(data);
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完成！");
    }
}